/*
 * Copyright 2007 Jeffrey Palm
 */

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

abstract class ReallyAbstractFinder {

  protected final void note(Object msg) {
    Util.note(this,msg);
  }


  protected boolean cancelled;
  public final boolean isCancelled() {
    return cancelled;
  }
  public final void cancel() {
    this.cancelled = true;
  }

  /** Save the cookies on every open */
  private Map cookies = new HashMap();

  {
    // these are my cookies for NYTimes
    String[] cs = getCookies();
    if (cs != null) {
      for (int i=0; i<cs.length; i+=2) cookies.put(cs[i],cs[i+1]);
    }
  }

  /**
   * Override this to use specific cookies
   */
  String[] getCookies() {return null;}

  protected final BufferedReader reader(String urlString) throws IOException {
    return new BufferedReader(new InputStreamReader(open(urlString)));
  }
  
  protected final InputStream open(String urlString) throws IOException {
    URL u = new URL(urlString);
    String ua = UserAgents.randomUserAgent();
    URLConnection con = u.openConnection();
    //con.setRequestProperty("User-Agent",ua);
    //
    // Create the cookie string
    //
    StringBuffer cookieStr = new StringBuffer();
    for (Iterator it = cookies.entrySet().iterator(); it.hasNext();) {
      Map.Entry e = (Map.Entry)it.next();
      cookieStr.append(String.valueOf(e.getKey()));
      cookieStr.append("=");
      cookieStr.append(String.valueOf(e.getValue()));
      if (it.hasNext()) cookieStr.append("; ");
    } 
    con.setRequestProperty("Cookie",cookieStr.toString());
    try {
      for (int i=1;;i++) {
        //
        // Taken from: http://www.hccp.org/java-net-cookie-how-to.html
        //
        String key = con.getHeaderFieldKey(i);
        if (key == null || "".equals(key)) break;
        if (!key.toLowerCase().equals("set-cookie")) continue;
        final String cookie = con.getHeaderField(key);
        String cookieSub = cookie.substring(0, cookie.indexOf(";"));
        String cookieName = cookieSub.substring(0, cookieSub.indexOf("="));
        String cookieValue = cookieSub.substring(cookieSub.indexOf("=") + 1, cookieSub.length());
        if (!cookies.containsKey(cookieName)) {
          cookies.put(cookieName,cookieValue);
        }
      }
    } catch (Exception e) {e.printStackTrace();}
    con.connect();
    note("Looking up url(" + u + ")" + " with agent " + ua);
		InputStream in = con.getInputStream();
    return in;
  }

}